package controller

import (
	"demo/common"
	"demo/models/po"
	"github.com/gin-gonic/gin"
	"strconv"
)

// 注册路由
//func RegisterRoomRoutes(r *gin.RouterGroup) {
//	r.POST("/rooms", AddRoom)
//	r.GET("/rooms/:id", GetRoomList)
//}

// 注册分组路由
func RegisterRoomRoutes(r *gin.Engine) {
	roomGroup := r.Group("/rooms")
	{
		roomGroup.POST("", AddRoom)
		roomGroup.GET("/:id", GetRoomList)
	}
}

func GetRoomList(c *gin.Context) {
	var room po.Room
	// 绑定参数
	idStr := c.Param("id")
	// 将字符串类型的 id 转换为整数类型的 Id
	roomId, err := strconv.Atoi(idStr)
	room.Id = roomId
	if err != nil {
		common.ReturnError(c, 500, "参数错误")
	}
	room, err = po.GetRoomById(room.Id)
	if err != nil {
		common.ReturnError(c, 500, "查询room列表失败")
	} else {
		common.ReturnSuccess(c, 200, "查询room成功", room, 1)
	}
}

// AddRoom 新增房间
func AddRoom(c *gin.Context) {
	var newRoom po.Room
	// 绑定参数
	if err := c.ShouldBindJSON(&newRoom); err != nil {
		common.ReturnError(c, 500, "参数错误")
	}

	// 调用业务逻辑层添加房间
	if err := po.AddRoom(&newRoom); err != nil {
		common.ReturnError(c, 500, "新增房间失败")
	} else {
		common.ReturnSuccess(c, 200, "新增房间成功", newRoom, 1)
	}
}
